package com.mice.view.game{
	import com.iyimo.control.CDButton;
	import com.iyimo.scene.BaseScene;
	import com.mice.event.GameEvent;
	import com.mice.meta.FighterMeta;
	import com.mice.model.fight.LevelInfoVO;

	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;

	[Event(name="START_STORY", type="com.mice.event.GameEvent")]

	public class GameScene extends BaseScene{
		/**
		 * 生产战士
		 */		
		private var _btn1:CDButton;

		/**
		 * 生产弓箭手
		 */		
		private var _btn2:CDButton;

		/**
		 * 生产法师
		 */		
		private var _btn3:CDButton;

		/**
		 * 能量
		 */		
		private var _energyTxt:TextField;

		/**
		 * 本关口信息
		 */		
		private var _vo:LevelInfoVO;

		/**
		 * 存储产兵按钮
		 */		
		private var _btnDict:Dictionary;

		public function GameScene(rawView:MovieClip){
			_btnDict = new Dictionary();
			super(rawView);
		}

		override protected function createView():void{
			_energyTxt = _view["txt_energy"]

			_btn1 = CDButton.createCDBtn(_view["warrier_btn"], new CircleMask(), 5);
			_btn1.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
			_btn1.data = 0;
			_btnDict[_btn1.data] = _btn1;

			_btn2 = CDButton.createCDBtn(_view["arrow_btn"], new CircleMask(), 5);
			_btn2.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
			_btn2.data = 1;
			_btnDict[_btn2.data] = _btn2;

			_btn3 = CDButton.createCDBtn(_view["magic_btn"], new CircleMask(), 5);
			_btn3.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
			_btn3.data = 2;
			_btnDict[_btn3.data] = _btn3;
		}

		/**
		 * 设置战场信息,由此来更新关卡数据
		 * _fighterMetaList--本关卡,生产的兵种对于的meta
		 */		
		public function setFightInfo(value:LevelInfoVO):void{
			_vo = value;
		}

		/**
		 * 用精力值来更新btn状态
		 */		
		public function updateBtnStatus(energy:int):void{
			_energyTxt.text = energy.toString();

			for each (var btn:CDButton in _btnDict){
				var meta:FighterMeta = _vo.fighterMetaList[btn.data];
				if (meta.costEnergy > energy)
					btn.enable = false;
				else
					btn.enable = true;
			}
		}

		private function onClick(e:MouseEvent):void{
			var mc:MovieClip = e.currentTarget as MovieClip;
			var btn:CDButton = _btnDict[mc.data] as CDButton;
			if (btn.isCD)return;

			var ev:GameEvent = new GameEvent(GameEvent.SPAWN_FIGHTER);
			ev.data = _vo.fighterMetaList[btn.data];
			dispatchEvent(ev);
		}
	}
}

